package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysDistricts;
import com.ruoyi.system.service.ISysDistrictsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 地区 信息操作处理
 *
 * @author ruoyi
 * @date 2018-12-19
 * 3d7d66f0-d16b-4ba1-a386-82880cc85e4b
 */
@Controller
@RequestMapping("/system/districts")
public class SysDistrictsController extends BaseController {
    private String prefix = "system/districts";

    @Autowired
    private ISysDistrictsService SysDistrictsService;

    @RequiresPermissions("system:districts:view")
    @GetMapping()
    public String SysDistricts() {
        return prefix + "/districts";
    }

    @RequiresPermissions("system:districts:list")
    @GetMapping("demo")
    public String demo() {
        return prefix + "/demo";
    }

    /**
     * 查询地区列表
     * 72009f00-249a-4e4e-adf3-f9ebeeb7df5d
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysDistricts SysDistricts) {
        startPage();
        List<SysDistricts> list = SysDistrictsService.selectSysDistrictsList(SysDistricts);
        return getDataTable(list);
    }


    /**
     * 导出地区列表
     * 7a8a7163-869b-4c70-92a1-03ec5bcd848b
     */
    @RequiresPermissions("system:districts:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysDistricts SysDistricts) {
        List<SysDistricts> list = SysDistrictsService.selectSysDistrictsList(SysDistricts);
        ExcelUtil<SysDistricts> util = new ExcelUtil<SysDistricts>(SysDistricts.class);
        return util.exportExcel(list, "districts");
    }

    /**
     * 新增地区
     * 8d9db2d6-71de-4da5-a134-216546b59d69
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存地区
     * f200c679-3241-41e3-a32a-53a9cab793e9
     */
    @RequiresPermissions("system:districts:add")
    @Log(title = "地区", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysDistricts SysDistricts) {
        SysDistricts.setPid(SysDistricts.getId() / 100);
        SysDistricts.setCreateTime(new Date());
        SysDistricts.setUpdateTime(new Date());
        SysDistricts.setOperator(ShiroUtils.getLoginName());
        return toAjax(SysDistrictsService.insertSysDistricts(SysDistricts));
    }

    /**
     * 修改地区
     * c43f6b84-f004-4b61-b070-d49df1947007
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        SysDistricts SysDistricts = SysDistrictsService.selectSysDistrictsById(id);
        mmap.put("districts", SysDistricts);
        return prefix + "/edit";
    }

    /**
     * 修改保存地区
     * add22ea8-f616-42e4-9807-a4353bffd338
     */
    @RequiresPermissions("system:districts:edit")
    @Log(title = "地区", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysDistricts SysDistricts) {
        SysDistricts.setPid(SysDistricts.getId() / 100);
        SysDistricts.setOperator(ShiroUtils.getLoginName());
        SysDistricts.setUpdateTime(new Date());
        return toAjax(SysDistrictsService.updateSysDistricts(SysDistricts));
    }

    /**
     * 删除地区
     * cb243883-4fcd-40ff-aeb5-3898249e295b
     */
    @RequiresPermissions("system:districts:remove")
    @Log(title = "地区", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(SysDistrictsService.deleteSysDistrictsByIds(ids));
    }

}
